import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import {BtDataService} from "../../bt-data.service";

@IonicPage()
@Component({
  selector: 'page-bs-match-list',
  templateUrl: 'bs-match-list.html',
})
export class BsMatchListPage {

    code;
    countryId;
    countryName;
    constructor(public navCtrl: NavController, public navParams: NavParams, public btDataService:BtDataService) {

    }

    ionViewDidLoad() {
        this.code = this.navParams.get('code') || 1;
        this.countryId = this.navParams.get('id') || undefined;
        this.countryName = this.navParams.get('name');
        this.getLeagueList(this.code,1);

    }
    curPage = 1;
    leagueList;
    loading;
    getLeagueList(code,pageNo,event?){
        if(pageNo == 1)
            this.loading = true;
        this.btDataService.getBkLeagueList(code,pageNo,this.countryId)
            .then(res=>{
                if(pageNo == 1)
                this.loading = false;
                event && event.complete();
                if(res.hasOwnProperty('code') && res.code == '1'){
                    if(pageNo == 1){
                        this.leagueList = res.leagueList;
                        this.hasNext = true;
                    }else if(res.leagueList && res.leagueList.length > 0){
                        this.leagueList.push(...res.leagueList);
                        this.hasNext = true;
                    }else{
                        this.hasNext = false;
                    }
                    return;
                }
                console.error("获取联赛列表失败!");
            }).catch(e=>{
            this.loading = false;
            event && event.complete();
            console.error("获取联赛接口异常：",e);
        });
    }
    //获取下一页数据
    hasNext;
    loadNextPage($event){
        ++this.curPage;
        this.getLeagueList(this.code,this.curPage,$event);
    }

    //联赛首页，leagueId
    goLeaguePage(leagueId){
        this.navCtrl.push('LeagueTabsPage',{leagueId});
    }

}
